package operation;

import book.Book;
import book.Booklist;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-18
 * Time: 8:17
 */
public class DelOperation implements IOperation {
    //之前的删除操作写的有点瑕疵
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书！");
        System.out.println("请输入你要删除的书名：");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine();
        int index = 0;
        for (int i = 0; i < booklist.getUseSize(); i++) {
            if(name.equals(booklist.getBooks(i).getName())){
                index = i;
                break;
            }else{
                index = -1;
            }
        }
        if(index == -1){
            System.out.println("没有这本书");
            return;
        }

        for (int i = index; i < booklist.getUseSize()-1; i++) {
            booklist.setBooks(i,booklist.getBooks(i+1));
        }
        booklist.setUseSize(-1);
        System.out.println("删除成功！");
    }
}
